// pages/goodsDetail/goodsDetail.js
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		activity: '', //活动id
		up: null, //公司id
		attention: '关注',
		attentionId: null,
		shopData: null, //公司信息
		show: false,
		move: 0,
		right: false,
		boxArr: [], //生成的大盒子，共9个大盒子，每个大盒子有9个小盒子
		arrIndex: 0, //当前展示大盒子的index
		fication_id: '',
		avatar: '',
		balance: '',
		dialogShow: false,
		mobile: '',
		boxCover: '',
		boxTop: '',
		detailImg: []
	},
	goPage: app.goPage,
	//跳转个人商店
	shopOpen:function(){
		const _this = this
		wx.navigateTo({
			url: '/pages/shopDetail/index?id='+_this.data.up
		})
	},
	//点击关注按钮
	attentionClick() {
		const _this = this
		let url = '/v1/follow/follow'
		if (_this.data.attention == '已关注') {
			url = '/v1/follow/strick'
		}
		wx.request({
			url: app.globalData.url + url,
			method: 'POST',
			data: {
				login_id: _this.data.up,
				id: _this.data.attentionId,
				openid: app.globalData.openId
			},
			success: function(r) {
				console.log('点击关注----', r)
				if (r.data.code == 0) {
					console.log(_this.data.attention)
					_this.getlog(_this.data.up)
					if (_this.data.attention == '已关注') {
						_this.setData({
							attention: '关注'
						})
					} else {
						_this.setData({
							attention: '已关注'
						})
					}
				}
			}
		})
	},
	//获取公司信息
	getlog: function(id) {
		const _this = this
		wx.request({
			url: app.globalData.url + '/v1/follow/log',
			method: 'GET',
			data: {
				id: id,
				openid: app.globalData.openId
			},
			success: function(r) {
				console.log('获取公司信息----', r)
				let data = r.data
				console.log('获取公司信息----', data)
				if (data.code == 0) {
					if (data.data.fid) {
						_this.setData({
							attention: '已关注',
							attentionId: data.data.fid
						})
					}else{
						_this.setData({
							attention: '关注'
						})
					}
					_this.setData({
						shopData: data.data
					})
				}
			}
		})
	},
	getBalance: function() {
		var _this = this
		wx.request({
			url: app.globalData.url + '/v1/payment/show',
			method: 'GET',
			data: {
				openid: app.globalData.openId
			},
			success: function(r) {
				r = r.data
				if (r.code == 0) {
					_this.setData({
						balance: r.data.money
					})
				} else {
					wx.showToast({
						title: r.data.message,
						icon: 'none',
						duration: 2000
					})
				}
			}
		})
	},
	showDialog: function() {
		this.setData({
			dialogShow: !this.data.dialogShow
		})
	},
	getDetail: function(id) {
		var _this = this
		wx.request({
			url: app.globalData.url + '/v1/goods/details',
			method: 'GET',
			data: {
				openid: app.globalData.openId,
				fid: id
			},
			success: function(r) {
				r = r.data
				if (r.code == 0) {
					_this.setData({
						boxCover: r.data.image[0].url,
						boxTop: r.data.image[1].url,
						detailImg: r.data.details
					})
					console.log(_this.data.detailImg);
				} else {
					wx.showToast({
						title: r.data.message,
						icon: 'none',
						duration: 2000
					})
				}
			}
		})
	},
	showDetail() {
		this.setData({
			show: !this.data.show
		})
		// wx.showToast({
		//   title: '123',
		// })
	},
	// 换一盒
	changeBox: function() {
		var _this = this
		this.setData({
			move: 1,
			showMsg: false,
			right: true
		})
		setTimeout(function() {
			_this.setData({
				move: 2,
				right: false
			})
			if (_this.data.arrIndex < _this.data.boxArr.length - 1) {
				_this.setData({
					arrIndex: _this.data.arrIndex + 1
				})
			} else {
				_this.setData({
					arrIndex: 0
				})
			}
			console.log(_this.data.arrIndex)
		}, 500)
	},
	open: function(e) {
		console.log(e)
		var index = e.currentTarget ? e.currentTarget.dataset.index : e
		app.globalData.bBoxIdx = this.data.arrIndex
		app.globalData.sBoxIdx = index
		wx.navigateTo({
			url: '/pages/box/box?fid=' + this.data.fication_id + '&activity=' + this.data.activity + '&ffid=' + this.data.ffid
		})
	},
	getPhone: function(r) {
		var idx = r.currentTarget.dataset.index
		var _this = this
		wx.showToast({
			title: 'loading...',
			icon: 'loading',
			duration: 60000
		})
		if (r.detail.errMsg == 'getPhoneNumber:ok') {
			wx.checkSession({
				success: function() {
					_this.sendMobile(app.globalData.session_key, r.detail.encryptedData, r.detail
						.iv, idx)
				},
				fail: function() {
					_this.login(r.detail.encryptedData, r.detail.iv, idx)
				}
			})
		} else {
			wx.hideToast()
		}
	},
	login: function(encryptedData, iv, idx) {
		wx.login({
			success: function(res) {
				wx.request({
					url: app.globalData.url + '/v1/wechat/accesstoken',
					method: 'GET',
					data: {
						code: res.code
					},
					success: function(r) {
						app.globalData.session_key = r.data.data.session_key
						app.globalData.openId = r.data.data.openid
						console.log(app.globalData.session_key)
						sendMobile(app.globalData.session_key, encryptedData, iv,
							idx)
					}
				})
			}
		})
	},
	sendMobile: function(session_key, encryptedData, iv, idx) {
		var _this = this
		wx.request({
			url: app.globalData.url + '/v1/wechat/mobile',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				encryptedData: encryptedData,
				iv: iv,
				openid: app.globalData.openId,
				session_key: session_key
			},
			success: function(res) {
				if (res.data.code == 0) {
					wx.hideToast()
					wx.showToast({
						title: '获取手机号成功！',
						icon: 'success',
						duration: 2000
					})
					_this.open(idx)
				} else {
					wx.hideToast()
					wx.showToast({
						title: res.data.data.message,
						icon: 'none',
						duration: 4000
					})
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		app.randomBox()
		console.log('options--------', options)
		this.setData({
			up: options.up
		})
		
		if (options.activity) {
			this.setData({
				activity: options.activity,
				ffid: options.ffid
			})
		}
		this.setData({
			boxArr: app.globalData.boxArr,
			fication_id: options.fid,
			avatar: app.globalData.userInfo.avatarUrl,
			mobile: app.globalData.mobile
		})

		this.getDetail(options.fid)
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getBalance()
		this.getlog(this.data.up)
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
